<!-- 文件所有权归北京优博泰克 -->
<!-- 最小试验运行单元 -->
<template>
    <div class="minimumCell" v-loading="loading">
        <!-- 表格 -->
        <dy-table :elTable_Options="elTable_Options" @handleClick="handleClick" @bottonHandle="bottonHandle"
            @handleSelect="handleSelect" ref="table" />
        <AddSubTaskLog ref="subTaskLog" v-if="modalStatus == '操作最小单元弹框'" @gettabledata="gettabledata"/>
    </div>
</template>

<script setup>
import { reactive, toRefs, ref, onMounted, nextTick } from 'vue'
import { useCach } from '@/store/cach'
import { ElMessageBox, ElMessage } from 'element-plus'
import api from '@/api'
import { computed } from 'vue'

import AddSubTaskLog from './minimumCell/addSubTask.vue'

onMounted(() => {
    gettabledata()
})

let pageCach = useCach()

//数据
let state = reactive({
    loading: false
})
let { loading } = toRefs(state)

//实例
let table = ref(null)
let subTaskLog = ref(null)

let modalStatus = computed(() => pageCach.modalStatus)

let allConfigList = [
    {
        label: "编号",
        prop: "code",
        columType: 'text'
    },
    {
        label: "子任务",
        prop: "subtask",
        columType: 'text'
    },
    {
        label: "运行方法",
        prop: "runmethod",
        columType: 'text'
    },
    {
        label: "参考标准",
        prop: "reference",
        columType: 'text'
    },
    {
        label: "类型",
        prop: "elementtype",
        columType: 'text'
    },
    {
        label: "备注",
        prop: "remark",
        columType: 'text'
    },
    {
        label: "任务类别",
        prop: "tabletype",
        columType: 'text'
    },
    {
        label: "操作",
        prop: "opear",
        columType: 'opreatebar',
        fixed: 'right',
        width: 120,
        operation: [
            {
                op_name: '编辑',
                op_index: 4,
                color: 'rgb(19, 206, 102)'
            },
            {
                op_name: '删除',
                op_index: 5,
                color: '#f56c6c'
            }
        ]
    }
]

//表格数据
let elTable_Options = reactive({
    pagename: '最小试验运行单元', //页面名称
    selection: false, //true 单选
    allConfigList: allConfigList, //需要配置项就加 不需要可以不写
    eltableArr: allConfigList, //表头数据
    table_type: "standardcase_最小试验运行单元", //列表配置喜好 和 allConfigList一起
    getwayname: 'testelement', //接口名
    uid: 'testelement_table',
    //按钮
    opbutton: [{
        label: '新增case',
        dealway: 'addSubTask',
        icon: 'Plus',
        type: "primary",
        keyV: "add",
        disabled: false
    }, {
        label: "批量导入",
        dealway: "importData",
        icon: 'Download',
        type: "",
        keyV: "import",
        disabled: false
    }, {
        label: "批量导出",
        dealway: "exportData",
        icon: 'Upload',
        type: "",
        keyV: "export",
        disabled: false
    }, {
        label: "批量删除",
        dealway: "asysdeleterow",
        icon: 'Delete',
        type: "danger",
        keyV: "deleteT",
        disabled: false
    }],
    //查询栏
    searchList: [{
        label: "搜索子任务丶运行方法丶参考标准丶类型",
        key: "search",
        searchtype: 'input',
        width: 280
    }],
    /* tab表格菜单 */
    menuname: 'property',
    elMenuArr: [
        {
            key: 'property',
            value: '性能',
            addConditions: [
                {
                    key: "tabletype",
                    value: "性能"
                }
            ]
        },
        {
            key: 'reliability',
            value: '可靠性',
            addConditions: [
                {
                    key: "tabletype",
                    value: "可靠性"
                }
            ]
        },
        {
            key: 'SystemCom',
            value: '系统零部件',
            addConditions: [
                {
                    key: "tabletype",
                    value: "系统零部件"
                }
            ]
        },
        {
            key: 'speed',
            value: '变速箱-传动',
            addConditions: [
                {
                    key: "tabletype",
                    value: "变速箱-传动"
                }
            ]
        },
        {
            key: 'clutch',
            value: '离合器-传动',
            addConditions: [
                {
                    key: "tabletype",
                    value: "离合器-传动"
                }
            ]
        },
        {
            key: 'drive',
            value: '驱动桥-传动',
            addConditions: [
                {
                    key: "tabletype",
                    value: "驱动桥-传动"
                }
            ]
        }
    ],
    /* 导出导出参数配置 */
    importexport: {
        //导入参数
        import_templatename: 'TestElement',
        import_filename: '最小试验单元模板.xls',
        import_api: 'importtestelement',
        import_other_params: {
            "tabletype": '发动机'
        },
        // 导出参数
        export_filename: '最小试验单元.xls',
        export_api: 'exportele'
    }
})

//获取数据
let gettabledata = () => {
    if (pageCach.tableInfo && pageCach.tableInfo.uid == elTable_Options.uid) {
        elTable_Options.menuname = pageCach.tableInfo.tabName
        table.value.setSearchData(pageCach.tableInfo.searchParams)
        nextTick(() => {
            table.value.getTableData(pageCach.tableInfo)
        })
        return
    }
    nextTick(() => {
        table.value.getTableData()
    })
}

//链接点击
let handleClick = (row, index) => {
    if (index == 4) {
        // 编辑
        pageCach.changeShowModal(true)
        pageCach.changeModalStatus('操作最小单元弹框')
        let corresponding = elTable_Options.elMenuArr.filter(it => it.key == elTable_Options.menuname)
        nextTick(() => {
            subTaskLog.value.openFunction({
                title: '编辑最小单元子任务',
                tab: corresponding[0].value,
                row
            })
        })
        return
    }

    //删除
    if (index == 5) {
        ElMessageBox.confirm('确定删除当前项?', "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
        }).then(async () => {
            state.loading = true
            await api.changeaxiosdata('testelement', 'delete', { id: row.id })
            ElMessage({
                message: '删除成功!',
                type: 'success'
            })
            getData()
            state.loading = false

        }).catch(() => { })
    }
}

//按钮点击
let bottonHandle = (wayname) => {
    if (wayname == 'addSubTask') {
        addSubTask()
        return
    }

    if (wayname == 'asysdeleterow') {
        asysdeleterow()
        return
    }
}

let addSubTask = () => {
    pageCach.changeShowModal(true)
    pageCach.changeModalStatus("操作最小单元弹框")
    let corresponding = elTable_Options.elMenuArr.filter(it => it.key == elTable_Options.menuname)
    nextTick(() => {
        subTaskLog.value.openFunction({
            title: '新增最小单元子任务',
            tab: corresponding[0].value
        })
    })
}

let asysdeleterow = () => {
    let selectArr = table.value.getTableSelection()
    if (!selectArr.length) {
        ElMessage({
            message: '请选择删除项',
            type: 'warning'
        })
        return
    }

    let ids = selectArr.map(it => it.id)
    ElMessageBox.confirm(`确认删除选中${ids.length}项?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
    }).then(async () => {
        state.loading = true
        let res = await api.changeaxiosdata('batch_delete_TestElement', 'post', ids)
        if (res) {
            ElMessage({
                message: '操作成功!',
                type: 'success'
            })
            getData()
        }
        state.loading = false
    }).catch(() => { })
}

//切换tab
let handleSelect = (key) => {
    elTable_Options.menuname = key
    nextTick(() => {
        table.value.conditional()
    })
}
</script>

<style scoped lang="scss">
.object {
    width: 100%;
    height: 100%;
}
</style>